// pages/index/index.js
const common = require('../../utils/common.js');
const play = require('../../utils/play.js');
const AI = require('../../utils/AI.js');
const bill = require('../../utils/bill.js');
const gambit = require('../../utils/gambit.js');
const rl = require('../../utils/rl.js');

Page({
  data: {
    activeTab: 'play',
    chessBoard: null,
    gameButtons: [
      { id: 'tyroPlay', text: '新手水平', type: 'offensivePlay' },
      { id: 'superPlay', text: '中级水平', type: 'offensivePlay' },
      { id: 'masterPlay', text: '大师水平', type: 'offensivePlay', disabled: true },
      { id: 'regretBn', text: '悔棋', type: 'regret' },
      { id: 'billBn', text: '重新开始', type: 'billBn' },
      { id: 'stypeBn', text: '放大棋局', type: 'stypeBn' }
    ],
    humanPlaceholder: 'Teaching Game',
    aiPlaceholder: 'Simple Style (2d)',
    billList: [],
    billBox: []
  },

  onLoad: function (options) {
    // Initialize the RL module
    rl.init();
    
    // Initialize game
    this.initGame();
  },

  initGame: function() {
    // Initialize chess board
    common.init();
    play.init();
    
    // Update UI
    this.setData({
      chessBoard: common.chessBoard
    });
  },

  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab
    });
  },

  handleGameButton: function(e) {
    const buttonId = e.currentTarget.dataset.id;
    
    switch(buttonId) {
      case 'tyroPlay':
        // Handle beginner level
        wx.showToast({
          title: '新手水平',
          icon: 'none'
        });
        break;
      case 'superPlay':
        // Handle intermediate level
        wx.showToast({
          title: '中级水平',
          icon: 'none'
        });
        break;
      case 'regretBn':
        // Handle regret move
        wx.showToast({
          title: '悔棋',
          icon: 'none'
        });
        break;
      case 'billBn':
        // Handle restart
        this.initGame();
        wx.showToast({
          title: '重新开始',
          icon: 'none'
        });
        break;
      case 'stypeBn':
        // Handle zoom
        wx.showToast({
          title: '放大棋局',
          icon: 'none'
        });
        break;
    }
  },

  onCanvasTap: function(e) {
    // Handle canvas touch events for chess moves
    const x = e.detail.x;
    const y = e.detail.y;
    
    // Process move logic here
    console.log('Canvas tapped at:', x, y);
  }
});